clear all;
clc;

load mnist_train;

sc = [0,1;0,2;0,3;0,4;0,5;0,6;0,7;0,8;0,9;1,2;1,3;1,4;1,5;1,6;1,7;1,8;1,9;2,5;2,6;2,7;2,9;3,4;3,6;3,7;3,9;4,6;4,7;5,7;5,9;6,7;6,8;6,9;7,8];
ic = [2,3;2,4;2,8;3,5;3,8;4,5;4,8;4,9;5,6;5,8;7,9;8,9];
nsc = size(sc,1);
nic = size(ic,1);

C = 1;

Y = yL';
Xt = xL';
clear xL;
clear yL;

l = size(Y,1);
[n,d] = size(Xt);

cls = unique(Y);
c = size(cls,1);

sz = zeros(c,1);
idx = cell(c,1);
for i = 1:c
    idx{i}=find(Y==cls(i));
    sz(i)=size(idx{i},1);
end

ix = cell(c,c);
Y = cell(c,c);

w = zeros((nsc+nic),d);
b = zeros((nsc+nic),1);

for k = 1:nsc
    i = sc(k,1)+1;
    j = sc(k,2)+1;
    disp(['Training pair ',int2str(i),' vs ',int2str(j)]);
    ix = [idx{i};idx{j}];
    X = Xt(ix,:);
    Y = [ones(sz(i),1);-ones(sz(j),1)];
    [w(k,:),b(k)] = primalSVM(X,Y);
end

for k = 1:nic
    i = ic(k)+1;
    j = ic(k)+1;
    disp(['Training pair ',int2str(i),' vs ',int2str(j)]);
    ix = [idx{i};idx{j}];
    X = Xt(ix,:);
    Y = [ones(sz(i),1);-ones(sz(j),1)];
    [w(k+nsc,:),b(k+nsc)] = primalSVM(X,Y,C);
end